﻿using System;
using System.Data;
using System.Windows.Forms;

namespace hello_accinv_New_
{
    public partial class UserSettingForm : Form
    {
        private Database db;

        public enum User
        {
            Administrator = 0,
            Manager = 1,
            SaleEmployee = 2
        }

        private enum Action
        {
            Add, Edit
        }
        
        public UserSettingForm()
        {
            InitializeComponent();

            db = new Database();

            LoadUsers();

            treeView.ExpandAll();
        }

        private void LoadUsers()
        {
            DataTable dt = db.Select("Firstname, Lastname, Permission, Username", "Users", "1");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["Permission"].ToString())
                {
                    case "0":   // Administrator
                        treeView.Nodes["Administrator"].Nodes.Add(
                            dt.Rows[i]["Username"].ToString(),
                            dt.Rows[i]["Firstname"].ToString() + " " + dt.Rows[i]["Lastname"].ToString()
                        );
                        break;
                    case "1":   // Manager
                        treeView.Nodes["Manager"].Nodes.Add(
                            dt.Rows[i]["Username"].ToString(),
                            dt.Rows[i]["Firstname"].ToString() + " " + dt.Rows[i]["Lastname"].ToString()
                        );                        break;
                    case "2":   // Sale Employee
                        treeView.Nodes["Sale Employee"].Nodes.Add(
                            dt.Rows[i]["Username"].ToString(),
                            dt.Rows[i]["Firstname"].ToString() + " " + dt.Rows[i]["Lastname"].ToString()
                        );
                        break;
                }
            }
            dt = null;
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView.SelectedNode.IsSelected)
            {
                if (treeView.SelectedNode.Level == 0)
                {
                    DisplayTypePanel();

                    toolStripAddUser.Enabled = true;
                    toolStripRemoveUser.Enabled = false;
                }
                else if (treeView.SelectedNode.Level == 1)
                {
                    LoadUserInformation();
                    DisplayUserPanel();

                    toolStripAddUser.Enabled = false;
                    toolStripRemoveUser.Enabled = true;
                }
            }
            else
            {
                panelUserType.Visible = false;
                panel2.Visible = false;
            }
        }

        private void DisplayTypePanel()
        {
            label2.Text = treeView.SelectedNode.Text;
            panelUserType.Visible = true;
            panel2.Visible = false;
        }

        private void LoadUserInformation()
        {
            // Load user information
            DataTable dt = db.Select("Title, Firstname, Lastname, BirthDate, Email, HomeTel, MobileTel, Username, Passwd", "Users", "Username = '" + treeView.SelectedNode.Name + "'");
            cmbTitle.SelectedItem = dt.Rows[0]["Title"].ToString();
            txtboxFirstname.Text = dt.Rows[0]["Firstname"].ToString();
            txtboxLastname.Text = dt.Rows[0]["Lastname"].ToString();
            dtBirthDate.Value = DateTime.Parse(dt.Rows[0]["BirthDate"].ToString());
            txtboxEmail.Text = dt.Rows[0]["Email"].ToString();
            txtboxHomeTel.Text = dt.Rows[0]["HomeTel"].ToString();
            txtboxMobileTel.Text = dt.Rows[0]["MobileTel"].ToString();
            txtboxUsername.Text = dt.Rows[0]["Username"].ToString();
            txtboxPassword.Text = dt.Rows[0]["Passwd"].ToString();
            lbUserType.Text = treeView.SelectedNode.Parent.Text;
            dt = null;
        }
        
        private void DisplayUserPanel()
        {
            panel2.Visible = true;
            panelUserType.Visible = false;
        }

        private void AddNewUser()
        {
            ClearUserForm();
            DisplayUserPanel();
            lbUserType.Text = treeView.SelectedNode.Text;
        }

        private void ClearUserForm()
        {
            btnEditPassword.Enabled = true;
            cmbTitle.SelectedIndex = -1;
            dtBirthDate.Value = DateTime.Today;
            txtboxFirstname.Clear();
            txtboxLastname.Clear();
            txtboxHomeTel.Clear();
            txtboxMobileTel.Clear();
            txtboxUsername.Clear();
            txtboxPassword.Clear();
        }

        private void RemoveUser()
        {

        }

        private void toolStripAddUser_Click(object sender, EventArgs e)
        {
            if (treeView.SelectedNode.Level == 0)
            {
                if (MessageBox.Show("Add new user into " + treeView.SelectedNode.Text + " type?", "Add new user", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == System.Windows.Forms.DialogResult.Yes)
                {
                    AddNewUser();
                }
            }
        }

        private void toolStripRemoveUser_Click(object sender, EventArgs e)
        {
            if (treeView.SelectedNode.Level == 1)
            {
                // Compare selected user with current login name
                if (treeView.SelectedNode.Text == "")
                {
                    MessageBox.Show("This user is using this system.","Delete User",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
                else if (MessageBox.Show("Do you want to remove user\n\nName: " + treeView.SelectedNode.Text + "\n\nThis user won't be able to user this system anymore.", "Remove user", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) 
                    == System.Windows.Forms.DialogResult.Yes)
                {
                    RemoveUser();
                }
            }
        }

        private void btnEditPassword_Click(object sender, EventArgs e)
        {
            btnEditPassword.Enabled = false;
            txtboxPassword.Enabled = true;
            txtboxPassword.SelectAll();
        }

        private void chkboxShowCharacters_CheckedChanged(object sender, EventArgs e)
        {
            if (chkboxShowCharacters.Checked)
            {
                txtboxPassword.UseSystemPasswordChar = false;
            }
            else
            {
                txtboxPassword.UseSystemPasswordChar = true;
            }
        }

        private void btnSaveChanges_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Confirm ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) 
                == System.Windows.Forms.DialogResult.Yes)
            {
                db.Update("Users"
                    , "Passwd = '" + txtboxPassword.Text + "'," +
                      "Title = '" + cmbTitle.SelectedItem.ToString() + "'," +
                      "Firstname = '" + txtboxFirstname.Text + "'," +
                      "Lastname = '" + txtboxLastname.Text + "'," +
                      "HomeTel = '" + txtboxHomeTel.Text + "'," +
                      "MobileTel = '" + txtboxMobileTel.Text + "'," +
                      "Email = '" + txtboxEmail.Text + "'," +
                      "BirthDate = '" + CustomDateTime.ToSqlDate(dtBirthDate.Value) + "'"
                    , "Username = '" + txtboxUsername.Text + "'");

                MessageBox.Show("Updated");

                txtboxPassword.Enabled = false;
                btnEditPassword.Enabled = true;
            }
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Discard ?", "Discard", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == System.Windows.Forms.DialogResult.Yes)
            {
                LoadUserInformation();

                txtboxPassword.Enabled = false;
                btnEditPassword.Enabled = true;
            }
        }
    }
}